package k8s

import (
    "path/filepath"

    "k8s.io/client-go/kubernetes"
    "k8s.io/client-go/rest"
    "k8s.io/client-go/tools/clientcmd"
    "k8s.io/client-go/util/homedir"

    "cing.io/cing/internal/config"
)

var clientset *kubernetes.Clientset

func Clientset() *kubernetes.Clientset {
    return clientset
}

func initClientset() {
    k8sConfig, err := getConfig()
    if err != nil {
        panic(err.Error())
    }

    // creates the client
    clientset, err = kubernetes.NewForConfig(k8sConfig)
    if err != nil {
        panic(err.Error())
    }
}

func getConfig() (cfg *rest.Config, err error) {
    if config.LocalTest {
        cfg, err = clientcmd.BuildConfigFromFlags("", filepath.Join(homedir.HomeDir(), ".kube", "config"))
    } else {
        cfg, err = rest.InClusterConfig()
    }
    return
}
